package pers.zc.activiti.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pers.zc.activiti.utils.ActivitiUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/activiti")
public class ApiActivitiController {

    @Autowired
    ActivitiUtils activitiUtils;


    /**
     * 启动流程
     *
     * @param proceesKey 流程名字
     * @param userId     用户Id
     */
    @PostMapping("/startProcees")
    @ResponseBody
    public void startProceesInstance(String proceesKey, String userId) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("userID", userId);
        activitiUtils.startProceesInstance(proceesKey, variables);
    }

    /**
     * 未处理的流程（待办流程）
     *
     * @param userId   用户id
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @return
     */
    @PostMapping(value = "/needProcees", produces = "application/json")
    @ResponseBody
    public List needProcees(String userId, int pageNum, int pageSize) {
        List<Task> list = activitiUtils.queryCurretUserTaskByAssignerr(userId, pageNum, pageSize);
        List<Map> resultList = new ArrayList<>();
        for (Task tk : list) {
            Map map = new HashMap();
            map.put("taskName", tk.getName());
            map.put("id", tk.getId());
            map.put("userName", tk.getAssignee());
            map.put("createTime", tk.getCreateTime());
            resultList.add(map);
        }
        return resultList;
    }

    /**
     * 认领任务
     *
     * @param userId   用户id
     * @return
     */
    @PostMapping(value = "/claimTask", produces = "application/json")
    @ResponseBody
    public Object claimTask(String taskId,String userId) {
        activitiUtils.claimTask(taskId,userId);
        return "";
    }

    /**
     * 可选的任务（可选流程）
     *
     * @param userId   用户id
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @return
     */
    @PostMapping(value = "/chooseProcees", produces = "application/json")
    @ResponseBody
    public List chooseProcees(String userId, int pageNum, int pageSize) {
        List<Task> list = activitiUtils.queryCurretUserTaskByCandidate(userId, pageNum, pageSize);
        List<Map> resultList = new ArrayList<>();
        for (Task tk : list) {
            Map map = new HashMap();
            map.put("taskName", tk.getName());
            map.put("id", tk.getId());
            map.put("userName", tk.getAssignee());
            map.put("createTime", tk.getCreateTime());
            resultList.add(map);
        }
        return resultList;
    }

    /**
     * 已处理的流程（已办流程）
     *
     * @param userId   用户id
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @param finished 流程是否已完结
     * @return
     */
    @PostMapping(value = "/historyProcees", produces = "application/json")
    @ResponseBody
    public List historyProcees(String userId, int pageNum, int pageSize, Integer finished) {
        List<HistoricTaskInstance> list;
        if (finished == 1) {
            list = activitiUtils.queryHistoryUserTaskByAssignerrFinished(userId, pageNum, pageSize);
        } else {
            list = activitiUtils.queryHistoryUserTaskByAssignerrUnfinished(userId, pageNum, pageSize);
        }
        return list;
    }
    /**
     * 已处理的流程所有处理环节
     *
     * @param processInstanceId   流程实例id
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @param finished 流程是否已完结
     * @return
     */
    @PostMapping(value = "/historyAct", produces = "application/json")
    @ResponseBody
    public List historyActInstanceList(String processInstanceId, int pageNum, int pageSize, Integer finished) {
        List<HistoricActivityInstance> list;
        if (finished == 1) {
            list = activitiUtils.historyActInstanceListFinished(processInstanceId, pageNum, pageSize);
        } else {
            list = activitiUtils.historyActInstanceListUnfinished(processInstanceId, pageNum, pageSize);
        }
        return list;
    }

    /**
     * 执行下一步任务
     *
     * @param taskId 流程名字
     * @param userId 用户Id
     */
    @PostMapping("/nextProcees")
    public void nextProcees(String taskId, String userId, String status) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("userID", userId);
        variables.put("status", status);
        activitiUtils.findNextTaskDefinition(taskId);

        activitiUtils.finishCurrentTaskByTaskId(taskId, variables);
    }
}
